<?php

namespace api\modules\v1\controllers\member;

use Yii;
use api\controllers\UserAuthController;
use common\enums\StatusEnum;
use common\models\backend\AdminBank as BackendAdminBank;
use common\models\member\Recharge;
use yii\web\NotFoundHttpException;
use common\helpers\ResultHelper;

class RechargeController extends UserAuthController
{
    /**
     * @var Recharge
     */
    public $modelClass = Recharge::class;

    public function actionList($limit = 10 , $offset = 0)
    {
        $user= Yii::$app->user->identity->member;
        $list = $this->modelClass::find()
            ->where(['member_id'=>$user->id])
            ->orderBy('id desc')
            ->asArray()
            ->limit($limit)->offset($offset*$limit)->asArray()
            ->all();
        return ResultHelper::json(200,'ok', [
            'list'=>$list,
            'type'=>Recharge::$ActionType,
            'status'=>Recharge::$tatusName,
        ]);
    }
    
    public function actionTijiao()
    {   
        // $user= Yii::$app->user->identity;
        $user= Yii::$app->user->identity->member;
        $model = new Recharge();
        $model->setScenario('submit');
        $model->member_id = $user->id;
        $model->created_at = time();
        $connection = Yii::$app->db;
        $transaction = $connection->beginTransaction();
        try {
            $time = time() - 30;
            $count = Recharge::find()->where(['member_id'=>$user->id])->andWhere(['>','created_at',$time])->count();

            if ($count) {
                return ResultHelper::json(422,Yii::t('app','操作频繁，请30秒后再试'));
            }
            if (Yii::$app->request->post()) {
                $model->attributes = Yii::$app->request->post();

                // $model->note = $model->generate();
                if ( $model->save() ) {

                    $transaction->commit();
                    return ResultHelper::json('200','ok',[
                        'amount'=>$model->amount,
                        'bank'=>[
                            'bank_name'=>$model->bank_name,
                            'bank_user_name'=>$model->bank_user_name,
                            'bank_address'=>$model->address,
                            'bank_img'=>$model->bank_img
                        ]
                        ]);
                }else{
                    return ResultHelper::json('422','ok',array_values($model->getFirstErrors())[0]);
                }
            } 
         } catch (\Exception $e) {
                return ResultHelper::json(422,'error', Yii::t('app',$e->getMessage()));
            
        }
    }

    public function actionUpdateImg()
    {
        $id = Yii::$app->request->post('id');
        $pay_img = Yii::$app->request->post('pay_img');
        $update = Recharge::updateAll(['pay_img'=>$pay_img],['id'=>$id, 'pay_img'=>'', 'member_id'=>Yii::$app->user->identity->member_id, 'status'=>Recharge::WEISHENHE]);
        if ($update) {
            return ResultHelper::json(200,'打款凭证提交成功');
        } else {
            return ResultHelper::json(422,'该订单已不需要上传凭证');
        }
    }

    /**
     * Finds the Recharge model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param integer $id
     * @return Recharge the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = Recharge::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }

    public function actionGetBanks()
    {   
        $list = BackendAdminBank::GetList();
        return ResultHelper::json('200','ok',$list);
    }

    public function actionBankList($pay_type=0)
    {   
        $list = BackendAdminBank::GetListByPayType($pay_type);
        return ResultHelper::json('200','ok',$list);
    }
}
